import requests
from bs4 import BeautifulSoup
import re



'''
Identification_English_verification_code
仅识别网页链接图片的验证码
'''
def Identification_English_verification_code(URL):
    # 伪装headers
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36',
        'Referer': 'http://lab.ocrking.com/',
        'Cookie':'Hm_lvt_042ea432617194de0e3698151e6eaac8=1656052046; Hm_lpvt_042ea432617194de0e3698151e6eaac8=1656052046; PHPSESSID=iu3muhkg0uivq84qnqr7fcnd83; o_h=a85651e5Qxumkmc1wWKOhoud17hYr1ipCQVLVLT%252BvlCUEHiDNe%252BsyAsq; Hm_lvt_1ce8387240c1517b2b9a4fce8c617d86=1656052050; Hm_lpvt_1ce8387240c1517b2b9a4fce8c617d86=1656052050',
        'Accept':'application/xml, text/xml, */*; q=0.01',
    }
    # 请求荷载数据
    data = {
    'url': URL,    # 图片链接   --放入要识别的验证码图片即可识别出来
    'service': 'OcrKingForCaptcha',   # 验证码类识别
    'language': 'eng',   # eng-识别英语   sim-识别简体    tra-识别繁体    jpn-识别日语   kor-识别韩语
    'charset': '3',     #识别英语才可以更改此段 如果不是识别英语，需要把值清空无   0-所有英文字符   1-所有纯数字   2-小写英文字母   3-大写英文字母   4-数字小写字母   5-数字大写字母   6-大写小写字母   7-数字大写小写   8-常用英文字符   9-网址和邮件类   10-$￥商城价格   11-手机电话类   12-数学公式计算
    'outputFormat':'',   # 返回参数为默认html  验证码类型不可更改只能输出默认
    'email': '',
    }
    # 请求网页并解析内容
    soup = BeautifulSoup(requests.post('http://lab.ocrking.com/do.html',headers=headers,data=data).content,'html.parser',from_encoding="utf-8")  #解析器
    # 将验证码图片保存下来
    f = open('验证码.png', 'wb')
    f.write(requests.get(re.findall('<srcfileheight="40"width="100">(.*?)</srcfile>', str(soup).replace('\n', '').replace(' ', ''))[0].encode('utf-8').decode('unicode_escape')).content)
    f.close()
    # 提取识别出来的内容
    English_verification_code = re.findall('<result>(.*?)</result>', str(soup).replace('\n', '').replace(' ', ''))[0].encode('utf-8').decode('unicode_escape')
    return English_verification_code

# 请求的手机号
SJH = '请填写手机号'

# 百度发送请求-验证码
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36',
    'Referer': 'https://www.baidu.com/',
    'Cookie':'PSTM=1656042101; BIDUPSID=8A13A2E15E95586CFAB6FC178074F71B; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BAIDUID=2E3FF381A3C10BE650594289ED0A89CF:FG=1; delPer=0; PSINO=6; H_PS_PSSID=36547_36625_36673_36454_31254_36453_36691_36167_36694_36697_36570_36075_26350_36469_36316_36650; BA_HECTOR=2l20042lag0hak2lak1hbacv215; ZFY=F24Hp45h0h202j:BYh0b6P:BnNb5yNGBaQGvznPktJoyc:C; BCLID=8734549255440444846; BDSFRCVID=thAOJeC62wpIPMQDRHGGMews_eKBHDOTH6aoLd2TkB3hTsOpIkYTEG0Pax8g0KubCpV-ogKK3gOTH4PF_2uxOjjg8UtVJeC6EG0Ptf8g0f5; H_BDCLCKID_SF=tbueVCDXtKD3qn5zq45HMtAt-fIL54FXKKOLV-bS0l7keq8CDRJ2yq8I0tONBUbxBCTfQM7HWho1sh72y5jHhUAODROW3jJ3QT79QpToyxbpsIJM-JAWbT8U5f5mbpKLaKviaKOjBMb1MMJDBT5h2M4qMxtOLR3pWDTm_q5TtUJMeCnTD-Dhe4tX-NFJq6LOtxK; BCLID_BFESS=8734549255440444846; BDSFRCVID_BFESS=thAOJeC62wpIPMQDRHGGMews_eKBHDOTH6aoLd2TkB3hTsOpIkYTEG0Pax8g0KubCpV-ogKK3gOTH4PF_2uxOjjg8UtVJeC6EG0Ptf8g0f5; H_BDCLCKID_SF_BFESS=tbueVCDXtKD3qn5zq45HMtAt-fIL54FXKKOLV-bS0l7keq8CDRJ2yq8I0tONBUbxBCTfQM7HWho1sh72y5jHhUAODROW3jJ3QT79QpToyxbpsIJM-JAWbT8U5f5mbpKLaKviaKOjBMb1MMJDBT5h2M4qMxtOLR3pWDTm_q5TtUJMeCnTD-Dhe4tX-NFJq6LOtxK; BAIDUID_BFESS=0339DF956FC36EE298E880F22CD5DDEF:FG=1; Hm_lvt_90056b3f84f90da57dc0f40150f005d5=1655103818,1656052061; HOSUPPORT=1; HOSUPPORT_BFESS=1; UBI=fi_PncwhpxZ%7ETaJc7iTXNp7e23hVnfinpgXgajdweOoF1JpeJrx6OBgBEsc2R2%7ECDTlE9ArU7ujAar96IBr; UBI_BFESS=fi_PncwhpxZ%7ETaJc7iTXNp7e23hVnfinpgXgajdweOoF1JpeJrx6OBgBEsc2R2%7ECDTlE9ArU7ujAar96IBr; BDRCVFR[X_XKQks0S63]=mk3SLVN4HKm; userFrom=null; ab_sr=1.0.1_ZDBiMDc3OGExYzA3NjMxZmM3NWUzN2U3YjkwZmE4NjMyMDc0NjgzMjNmYTc2YzRlMTVmZjFmY2ZlMDdkM2U1YzEyN2VkOWQ2N2Q3ZWFkMjk5NGI2NmJmMjY3ZjliMjY1NzE1NmRiNTUyOWU3M2M3MmY5NzEzNzBjMDYxNDFmNzY1M2Q0ZGJlZmU5ZDZjNDBmMWMzODk2NWQzNGM0YzRiYQ==; Hm_lpvt_90056b3f84f90da57dc0f40150f005d5=1656055940; pplogid=3603BA7NvdrPOQH8MpwHAgLmQJzAlf%2F%2FaFGDN4vH06WVDp%2F4bemf9GmHfGWMVP7733XKWWnvxPV17ZUFjB1tzFt7u%2B%2FfqGVxphqocGk3Z%2B5v8rA%3D; pplogid_BFESS=3603BA7NvdrPOQH8MpwHAgLmQJzAlf%2F%2FaFGDN4vH06WVDp%2F4bemf9GmHfGWMVP7733XKWWnvxPV17ZUFjB1tzFt7u%2B%2FfqGVxphqocGk3Z%2B5v8rA%3D',
    'Accept':'*/*',
}

baidu_URL = requests.get(f'https://passport.baidu.com/v2/api/senddpass?gid=A93F21E-A65B-4F4F-AF60-13DB12FE9F9E&username={SJH}&countrycode=&bdstoken=f7ff4d8df40a26d24fb984249a0a1050&tpl=mn&loginVersion=v4&flag_code=0&client=&mkey=&moonshad=3688f87b554fad4f354b301321f225c67&ds=3od4CfZ1Tn2kdT8Afj8cG37K62R78%2FPhdmafkARrE0B0A9OP7NcWMDsmG4tnKA2gimCPjYaeKqjtAZjkGcMTkYQsD1s4rGRJBlR2Te69AEen2NPuxxKmf8XSv1h4IkRCtwE%2FhhMgLtWCyQVpHNBw9Uhl7b1DxOuUpWoJWGnSVpA%2B5AVxBm%2B%2FdTB0xIek2917%2FfhjHWq0b9bippZ4rXH%2B3SjNaHrAQxY166s5zedwdxTw2JQk64TnwHlJSGnW4WR6vERxM6uCZgHQmusbbm5ZRTxagQhR7zRYf7t8FVgGuZp90YLilEeuWACTSiO70wvq9kvZWMRMni6QZNdMxJhUJsb24yj3tJ3EjbNQ0SAKkMgBo2wAb65%2FZxdkf9CtPC%2Fe4V0W4hONrkyeKrJ9lalI2fS0TqRCgwUVNZZiFmr46MxHu11AHXtRZVlKhJoYHBjmsXr%2BGC5grYcRRfSktnAHJNuzQzxqJ%2Bp1K5IxACKfmKyY9wzs7A40ZZfQjF6nKEvCWgvVVLJM78qyc7P9JuE4rt2fkDxBZ9KFWGmcH3U3ZPViIVwCQOXw0EtsH47L2hUaQi5ZnbJlNJVcPXD%2B8NWsOWUYguyYcm6khAbW7zy9TXORUd40W6tYMP%2BfFKD0iQ8d5n9zNc03%2F7ZEwBI1VhvKP1u9Vg2p5Nf8nA48Itlot5VA6JymIz%2B8GSJXtPTfcKrOeQVja4vCu%2BwixwWmCf4o2nvZIyc5nQ4tcaCiHYgfmnRMMiotVra6jdWrkigfkfu68wEtlQX%2F9QxD4qeXD201pXn1IVJz3DE2Nop%2FNRRNJEBJ%2B%2BYwLekMjpEcxxGDBcpnlGEVLDLiLxeLXWs%2B%2BHRSiAI4jzX%2BmbO5GDETsJQsW8aw9BHCHJBTQNzl38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3kOzG3Qko~C1AYrfPytkSZr1oYFou4Gy6hvFwQAuC5Dfo~DywfODCAN9y3PU-_yttNwtI719WKhIrBr7-dtko-rB__CrsLsY-t1pcC1qfC15TCvAdrvBcrkSZrvMyC1qyC1q~4kATCB__Grmrm--tkoTCkAY4kq~tkoJr1qYrvqcrm-~4vp-tkoJr1qYrvqZrq__&apiver=v3&tt=1656060139401&traceid=&time=1656060139&alg=v3&sig=TzdjQUhBVElDNVFVdzViTWVKRG42bzgxblRHc1JQVGpqUmpJYWZ0TDhEc2gzdTgxbVdXTlBHaDh0WEN4UllnRg%3D%3D&elapsed=6&shaOne=00965c9aa98f3720513bd41f90ce03106656042d&rinfo=%7B%22fuid%22%3A%22cca44e790fef9fe3769cc5e110b563c3%22%7D&callback=bd__cbs__l3g71b',headers=headers)
baidu_data = BeautifulSoup(baidu_URL.content,'html.parser',from_encoding="utf-8")  # 解析器
print(re.findall('"msg":"(.*?)"',str(baidu_data).replace('\n', '').replace(' ', ''))[0].encode('utf-8').decode('unicode_escape'))

if re.findall('"msg":"(.*?)"',str(baidu_data).replace('\n', '').replace(' ', ''))[0].encode('utf-8').decode('unicode_escape') == '请填写验证码':
    def baidu():
        vcodestr = re.findall('"vcodestr":"(.*?)"',str(baidu_data.encode('utf-8').decode('unicode_escape')).replace('\n', '').replace(' ', ''))[0].encode('utf-8').decode('unicode_escape')
        vcodesign = re.findall('"vcodesign":"(.*?)"',str(baidu_data.encode('utf-8').decode('unicode_escape')).replace('\n', '').replace(' ', ''))[0].encode('utf-8').decode('unicode_escape')
        YZM = Identification_English_verification_code(f'https://passport.baidu.com/cgi-bin/genimage?{vcodestr}')
        baidu_URL_2 = requests.get(
            f'https://passport.baidu.com/v2/api/senddpass?gid=A93F21E-A65B-4F4F-AF60-13DB12FE9F9E&username={SJH}&countrycode=&bdstoken=f7ff4d8df40a26d24fb984249a0a1050&tpl=mn&vcodestr={vcodestr}&vcodesign={vcodesign}&verifycode={YZM}&flag_code=0&loginVersion=v4&mkey=&moonshad=3688f87b554fad4f354b301321f225c67&dv=tk0.286036475521880941656056018576%40qqr0KqrvPYrm~A5AJ2ASuCDyw5AyhQrvuQDTFfID64M9y3tkM~rU-~Cd--tsJyOs-YrvodC2-~Cd--tsJyOs-YrvoyCT-~Cd--tsJyOs-YrvoJrU-~Cd--tsJyOs-Yrvp~C2-_vr0-wrvqfrd-frfpYrvPc4m~A5AJ2ASuCDyw5AyhQrvuQDTCWPyhXOzJ3QkSyrd-ZCkBYrfS-CK~A5AJ2ASuCDyw5AyhQrvuQDTCWPyCyMEyaL2-JCf5YrfBZtkAdr1oYFou4Gy6hvFwQAuC5Dfo~DywfODC9ID6aIU3kOzG3Qko~r1PYrfMztkPZC1PYFou4Gy6hvFwQAuC5Dfo~DywfODC9ID6aIU3kOzG3Qko~C1AYrfPytkSZr1oYFou4Gy6hvFwQAuC5Dfo~DywfODCAN9y3PU-_yttNwtI719WKhIrBr7-dtko-rB__CrsLsY-t1pcC1qfC15TCvAdrvBcrkSZrvMyC1qyC1q~4kATCB__Grmrm--tkoTCkAY4kq~tkoJr1qYrvqcrm-~4vp-tkoJr1qYrvqZrq__&apiver=v3&tt=1656060638322&traceid=&time=1656060638&alg=v3&sig=bGtQdm5OV01jMWtYcFk2cDlpci9JeU12ZDVKUG1DVU5TcDR1S3BKVXgvVzE0RUF0QUkwZElOajY4VHdhVXNNQg%3D%3D&elapsed=2&shaOne=0059a7eaa1f057a812abb4deada2f477654b6884&rinfo=%7B%22fuid%22%3A%22cca44e790fef9fe3769cc5e110b563c3%22%7D&callback=bd__cbs__k5az53',
            headers=headers)
        baidu_data_2 = BeautifulSoup(baidu_URL_2.content, 'html.parser', from_encoding="utf-8")  # 解析器
        print(baidu_data_2.encode('utf-8').decode('unicode_escape'))
        if re.findall('"msg":"(.*?)"',str(baidu_data_2).replace('\n', '').replace(' ', ''))[0].encode('utf-8').decode('unicode_escape') == '验证码错误':
            baidu() # 再次运行
    baidu()

else:
    if re.findall('"msg":"(.*?)"',str(baidu_data).replace('\n', '').replace(' ', ''))[0].encode('utf-8').decode('unicode_escape') == '发送过于频繁，请稍后再试':
        pass
    else:
        print('验证码已发送，无需绕过效验码！！')
